<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Single as UM;
use think\Db;
use think\Request;

class Single extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    /**
    * 章节试题列表
    */
    public function index()
    {
        
        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where);
        $count = $model->_count($where);
        $cates = \app\admin\model\Question::all()->toArray();
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
            'cates' => $cates,
        ]);
        
        return $this->fetch();
    }

    /**
     * 根据题库获取章节
     */
    public function getChapters(){
        $request      = $this->request->post();
        $cate         = $request['cate'];
        $cate_info    = Db::name('tk_chapters')->where('cate',$cate)->select();
        die(outputSuccess($cate_info, '删除成功'));
    }
    /**
     * 根据章节获取子章节
     */
    public function getChapterDetail(){
        $request      = $this->request->post();
        $chapter_cate         = $request['chapter_cate'];
        $cate_info    = Db::name('tk_chapter_detail')->where('chapter_cate',$chapter_cate)->select();
        die(outputSuccess($cate_info, '删除成功'));
    }

    /**
     * 真题列表
     */
    public function index_two()
    {

        $model = new UM();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list_two($where);
        $count = $model->_count_two($where);
        $chapter_details = Db::name('tk_chapter_detail')->where('chapter_cate',0)->select();
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
            'chapter_details'=>$chapter_details
        ]);

        return $this->fetch();
    }

    /**
    * 添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
//            if(!$model->_checkName($request['chapter_title'])){
//                die(outputError($model->getError()));
//            }

            $ret = $model->_create($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '添加成功'));
        }else{
            $cates = \app\admin\model\Question::all()->toArray();
            $this->assign([
                'cates' => $cates,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 编辑
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $model = new UM();
            $ret = $model->_modify($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $id = (int)$this->request->get('id');
            $user_model = new UM();
            $data = $user_model->_findOne($id);
            $this->assign(
                [
                    'data'=>$data,
                ]
            );
            return $this->fetch();
        }
    }
    
    /**
    * 删除
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');
            $model = new UM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new UM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    
   
    
    /**
    * 设置管理状态
    */
    public function set_status(){
        $request = $this->request->post();
        //var_dump($request);die;
        $model = new UM();
        $ret = $model->_setStatus($request);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
    
    
    /**
    * 查看设置详情
    */
    public function detail(){
        $id = (int)$this->request->get('id');
        
        $model = new UM();
        $info = $model->_findOne($id);
        $this->assign([
            'info' => $info
        ]);
        
        return $this->fetch();
    }

}
